/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vehicle.beans;

import gmail.SendMailOk;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import vehicle.bol.BrandBol;
import vehicle.bol.CarBOL;
import vehicle.bol.OrdersBol;
import vehicle.entity.Brands;
import vehicle.entity.Vehicles;
import vehicle.entity.Orders;

/**
 *
 * @author lab1
 */
@ManagedBean
@SessionScoped
public class CarBean {

    /** Creates a new instance of CartBean */
    private List<Vehicles> listvehicle;
    private String valueSearch = "";

    public String getValueSearch() {
        return valueSearch;
    }

    public void setValueSearch(String valueSearch) {
        this.valueSearch = valueSearch;
    }
    CarBOL bolcart = new CarBOL();

    public List<Vehicles> getListvehicle() {
        if (checksearch && !valueSearch.equals("")) {
            listvehicle = bolcart.searchCarByVehicleName("name", valueSearch);
            checksearch = false;
        } else if (searchtypeID && !typeVehicleID.equals("")) {
            listvehicle = bolcart.searchCarByVehicleName("id", typeVehicleID);
            // typeVehicleID="";
            //searchtypeID=false;
        } else {
            listvehicle = bolcart.showAllVehicle();
        }
        return listvehicle;
    }

    public String Index() {
        checksearch = false;
        typeVehicleID = "";
        searchtypeID = false;
        return null;
    }

    public CarBean() {
    }

    public String showDetailCar(Vehicles veshow) {
        return null;
    }
    boolean checksearch = false;

    public String searchCar() {
        checksearch = true;
        typeVehicleID = "";
        searchtypeID = false;
        return null;
    }
    String typeVehicleID = "";
    boolean searchtypeID = false;

    public String SearchIDTypeVehicleMenu(String typeID) {
        typeVehicleID = typeID;
        searchtypeID = true;
        return "index.xhtml";
    }
    private Vehicles vehi;
    private List<String> listAbum;

    public List<String> getListAbum() {
        return listAbum;
    }

    public void setVehi(Vehicles vehi) {
        this.vehi = vehi;
    }

    public Vehicles getVehi() {
        return vehi;
    }
    private List<Vehicles> listProductLienQuan;

    public List<Vehicles> getListProductLienQuan() {
        return listProductLienQuan;
    }

    public String showDetailVehicle(Vehicles ve1) {
        vehi = new Vehicles();
        listProductLienQuan = new ArrayList<Vehicles>();
        for (Vehicles veh : listvehicle) {
            if (veh.getTypeID() == ve1.getTypeID() && ve1 != veh) {
                listProductLienQuan.add(veh);
            }
        }
        vehi = ve1;
        listAbum = bolcart.showAbumByVehicleID(ve1.getVehicleID());
        return "vehicleDetail.xhtml";
    }

    public String dathang() {
        return "dondathang.xhtml";
    }
    private Orders ors = new Orders();

    public Orders getOrs() {
        return ors;
    }

    public void setOrs(Orders ors) {
        this.ors = ors;
    }

    public String payment() {
        String to = ors.getEmail();
       // String to = "banglv_a04448@fpt.aptech.ac.vn";
        String subject = "Order";
        String body = "";
        body = body + "Your Name :" + ors.getCusFullName() + "\n";
        body = body + "Address:" + ors.getAddress() + "\n";
        body = body + "Email :" + ors.getEmail() + "\n";
        body = body + "Phone :" + ors.getPhoneNumber() + "\n";
        body = body + "Comment :" + ors.getComments() + "\n";
        body = body + "Vehicle Name :" + vehi.getVehilceName() + "\n";
        body = body + "Price :" + vehi.getPrice() + "\n";
        body = body + "Date Regist :" + Calendar.getInstance().getTime() + "\n";
        body = body + "\nThanks your";

        ors.setPrice(vehi.getPrice());
        ors.setQuantity(1);
        ors.setVehicleID(vehi.getVehicleID());
        ors.setStatus(0);
        int a = OrdersBol.insertOrder(ors);
        int send = SendMailOk.sendMail(to, subject, body);
        vehi = new Vehicles();
        ors = new Orders();
        if (a == 1 && send==1) {
            return "regissuccess.xhtml";
        } else {
            return "fails.xhtml";
        }

    }

    public String paymentAdmin() {
        int a = 0;
        ors.setPrice(vehi.getPrice());
        ors.setQuantity(1);
        ors.setVehicleID(vehi.getVehicleID());
        ors.setStatus(0);
        a = OrdersBol.insertOrder(ors);
        vehi = new Vehicles();
        ors = new Orders();
        submitPayment = true;
        if (a == 1) {
            return "orderadmin.xhtml";
        } else {
            return "fails";
        }
    }
    private boolean submitPayment = true;

    public boolean isSubmitPayment() {
        return submitPayment;
    }

    public void setSubmitPayment(boolean submitPayment) {
        this.submitPayment = submitPayment;
    }

    public void choiceVehicle(Vehicles vi) {
        submitPayment = false;
        vehi = vi;
    }
    // Menu Khoang
    List<Brands> listNameBran;
    BrandBol bb = new BrandBol();

    public List<Brands> getListNameBran() {

        listNameBran = bb.getAllBrand();
        return listNameBran;
    }
}
